﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTAccess.Business.Member;
using DCTLib.Common;

public partial class AdminZone_MemberManagement : System.Web.UI.Page
{
    #region Properties
    private string Message
    {
        get { return lblError.Text; }
        set { lblError.Text = value; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Message = String.Empty;
        if (!IsPostBack)
        {
            BindDataGrid(0);
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            MemberFilter filter = GetMemberforSearch();
            DataTable dataTable = filter.GetMember();
            if (!ValidationHelper.HasRow(dataTable))
            {
                lblResult.Text = "No record found";
                DatatableHelper.HasNoRow(dataTable, "FullName", gvSearch);
                ControlLoader.BindDatagrid(gvSearch, filter.GetMember(), 0);
            }
            else
            {
                lblResult.Text = dataTable.Rows.Count + " result(s) found";
                ControlLoader.BindDatagrid(gvSearch, filter.GetMember(), 0);
            }
        }
        catch
        {
            Message = AppMessages.CANNOT_SEARCH;
        }
    }

    protected void gvSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        MemberFilter filter = GetMemberforSearch();
        ControlLoader.BindDatagrid(gvSearch, filter.GetMember(), e.NewPageIndex);
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtFullName.Text = "";
        BindDataGrid(0);
    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        //redirect to Member Register form 
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        //Lấy giá trị checkbox sẽ bị xóa
        string[] paramID = Request.Params.GetValues("checkDel");

        if (paramID == null || paramID.Length < 1)
            return;

        //Khai báo nếu bất kỳ member nào bị xóa
        bool hasDeletedUser = false;

        //loop
        for (int i = 0; i < paramID.Length; i++)
        {
            bool isDeleted = false;

            Member member = MemberController.GetMemberByID(int.Parse(paramID[i]));

            if (member != null)
                isDeleted = MemberController.Delete(member);

            if (hasDeletedUser || isDeleted)
                hasDeletedUser = true;
        }

        BindDataGrid(0);

        //Đưa ra thông báo khi bất kỳ thành viên nào bị xóa

        if (hasDeletedUser)
        {
            lblError.Text = "Bạn không thể xóa thành viên này được vì sẽ ảnh hưởng tới toàn bộ hệ thống";
        }
        else
        {
            lblError.Text = AppMessages.CANNOT_DELETE;
        }
    }

    private void BindDataGrid(int pageIndex)
    {
        MemberFilter filter = new MemberFilter();
        ControlLoader.BindDatagrid(gvSearch, filter.GetMember(), pageIndex);
        DataTable dataTable = filter.GetMember();
        lblResult.Text = dataTable.Rows.Count + " result(s) found";
    }

    private MemberFilter GetMemberforSearch()
    {
        MemberFilter filter = new MemberFilter();
        filter.FullName = txtFullName.Text;
        return filter;
    }
}
